<?php
class Page_Model_Category extends Zend_Db_Table_Abstract
{
	protected $_name = 'page_category';
	
	public function insert(array $data)
	{
		//TODO : code to clean data before insert here
		$data['date_created'] = date('Y-m-d H:i:s');
		$data['site_id'] = App_Functions::getSiteId();
		parent::insert($data);
	}
	
	public function update(array $data, $where = null)
	{
		$data['date_updated'] = date('Y-m-d H:i:s');
		parent::update($data, $where);
	}
	
	public function fetchById($id)
	{
		$row = $this->find((int)$id)->current()->toArray();		
		return $row;
	}
	
	public function hasChild($menuId)
	{
		return $this->_db->fetchOne("SELECT MAX(*) FROM page_category WHERE parent = " .(int)$menuId);
	}

	public function fetchParentCategories()
	{
		return $this->_db->fetchAll("SELECT * FROM page_category WHERE parent = 0 AND isvisible =1 ORDER BY `order`");
	}
	
	public function deleteById($id)
	{
		$data['date_deleted'] = date('Y-m-d H:i:s');
		$data['isvisible'] = 0;
		parent::update($data, 'id = '.(int)$id);
	}
}
